package com.sgb.member.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.member.MemberConfig;
import com.sgb.member.R;
import com.sgb.member.databinding.FragmentHomeOtherMemberBinding;
import com.sgb.member.di.DaggerMemberFragComponent;
import com.sgb.member.di.MemberFragModule;
import com.sgb.member.entity.MemberBean;
import com.sgb.member.entity.MemberPersonBean;
import com.sgb.member.ui.adapter.HomeOtherMemberAdapter;
import com.sgb.member.ui.adapter.PersonCommonAdapter;
import com.sgb.member.view_model.HomePersonMemberModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.XCustomTabLayout;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.xrecyclerview.LoadingMoreFooter;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class HomePersonMemberFragment extends AppLazyFragment {

    @Inject
    public HomePersonMemberModel sModel;
    private FragmentHomeOtherMemberBinding mBinding;
    private PersonCommonAdapter mMemberAdapter;

    private List<CommonPopEntity> mTabData;
    private List<CommonPopEntity> mTabData11;
    private String mNo = "";
    private String mNo11 = "";
    private String mOriNo = "";
    private int mCurPos;
    private CommonPopEntity mCurEntity;

    private boolean noMoreData;

    public static HomePersonMemberFragment newInstance(int position, CommonPopEntity entity) {
        HomePersonMemberFragment fragment = new HomePersonMemberFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("INDEX", position);
        bundle.putSerializable("ENTITY", entity);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMemberFragComponent.builder()
                .appComponent(appComponent)
                .memberFragModule(new MemberFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_home_other_member, container, false);
        mBinding.setLifecycleOwner(this);
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        mCurPos = getArguments().getInt("INDEX", 0);
        mCurEntity = (CommonPopEntity) getArguments().getSerializable("ENTITY");
        if ("需方会员".equals(mCurEntity.getTypeName())) {
            mCurPos = 0;
        } else if ("供方会员".equals(mCurEntity.getTypeName())) {
            mCurPos = 1;
        }
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        LoadingMoreFooter footer = (LoadingMoreFooter) mBinding.recyclerView.getFootView();
        footer.setNoMoreHint("已经到底啦~");
        mBinding.recyclerView.setPullRefreshEnabled(false);
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                loadData(true);
            }
        });
    }

    @Override
    protected void onLazyLoad() {
        if (getActivity() != null) {
            boolean hasTab = loadTabData();
            if (hasTab) {
                loadData(false);
            }
        }
    }

    public void refreshTabData() {
        if (mTabData != null && mTabData.size() > 0) {
            mTabData.clear();
            mBinding.tabLayout.removeAllTabs();
            mBinding.tabLayout.clearOnTabSelectedListeners();
            mBinding.tabLayout2.removeAllTabs();
            mBinding.tabLayout2.clearOnTabSelectedListeners();
        }
        mBinding.tabLayout.setVisibility(View.GONE);
        mBinding.tabLayout.setVisibility(View.GONE);
        loadTabData();
    }

    public void loadData(boolean isLoadMore) {
        if (noMoreData && isLoadMore) {
            closeRecyclerRefreshing();
            return;
        }
        initRecyclerAdapter();
        switch (mCurPos) {
            case 0:
                // 需方会员
                sModel.getJoinMembers(isLoadMore, mNo, mNo11, "2"); // 会员关系 1供方 2需方
                break;
            case 1:
                // 供方会员
                sModel.getJoinMembers(isLoadMore, mNo, mNo11, "1"); // 会员关系 1供方 2需方
                break;
            default:
                break;
        }
    }

    public String getTabNo(int tab) {
        String no = "";
        String name = mCurEntity.getPickerViewText();
        if ("会员商品".equals(name)) {
            if (tab == 0) { //会员商品第一个全部id默认为0，所以置空
                no = "";
            } else {
                no = mTabData.get(tab).getId();
            }
        } else {
            no = mTabData.get(tab).getNo();
        }
        return no;
    }

    public String getSubTabNo(int tab) {
        String no = "";
        if ("会员商品".equals(mCurEntity.getPickerViewText())) {
            if (tab == 0) { //会员商品第一个全部id默认为0，所以置空
                no = "";
            } else {
                no = mTabData11.get(tab).getId();
            }
        } else {
            no = mTabData11.get(tab).getNo();
        }
        return no;
    }

    public boolean loadTabData() {
        if (getActivity() != null) {
            if (mTabData == null || mTabData.size() == 0) {
                switch (mCurPos) {
                    case 0:
                    case 1:
                        // 供方会员
                        // 需方会员
                        sModel.getMemberTypeList(mCurPos);
                        break;
                    default:
                        break;
                }
                return false;
            }
            return true;
        }
        return false;
    }

    public void loadSubTabData() {
        switch (mCurPos) {
            case 0:
            case 1:
                // 供方会员
                // 需方会员
                sModel.getMemberTypeSubList(mNo);
                break;
            default:
                break;
        }
    }

    private void initRecyclerAdapter() {
        if (getActivity() == null) {
            return;
        }
        switch (mCurPos) {
            case 0:
            case 1:
                // 供方会员
                // 需方会员
                try {
                    if (mMemberAdapter == null) {
                        mMemberAdapter = new PersonCommonAdapter(getContext(), null, mCurPos, null);
                        mBinding.recyclerView.setAdapter(mMemberAdapter);
                        mMemberAdapter.setOnItemClick(new AdapterOnItemClick<MemberPersonBean>() {
                            @Override
                            public void onItemClick(MemberPersonBean memberPersonBean, int position) {
                                if (!TextUtils.isEmpty(memberPersonBean.companyNo)) {
                                    MemberConfig.getInstance().startWebsiteInfo(getActivity(), memberPersonBean.companyNo, 1);
                                } else {
                                    MToast.showToast(getActivity(), "店铺No为空");
                                }
                            }
                        });
                        mBinding.recyclerView.setAdapter(mMemberAdapter);
                    }
                } catch (Exception e) {
                    MLog.d("HomeOtherMemberFragment", "e : " + e.getMessage());
                }
                break;
            default:
                break;
        }
    }

    public void setTabLayoutData(List<CommonPopEntity> data) {
        mBinding.tabLayout.removeAllTabs();
        mBinding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mTabData = new ArrayList<>();
        if (data != null && data.size() > 0) {
            mBinding.tabLayout.setVisibility(View.VISIBLE);
            mTabData.addAll(data);
            for (int i = 0; i < mTabData.size(); i++) {
                mBinding.tabLayout.addTab(mBinding.tabLayout.newTab().setText(mTabData.get(i).getTypeName()));
            }
            mBinding.tabLayout.setNormalColor(R.color.color_title_small);
            mBinding.tabLayout.addOnTabSelectedListener(mTabData, new XCustomTabLayout.SimpleSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    mNo = getTabNo(tab.getPosition());
                    if (!TextUtils.isEmpty(mNo)) {
                        loadSubTabData();
                    } else {
                        setSubTabLayoutData(new ArrayList<>());
                    }
                }
            });
            mNo = getTabNo(0);
            if (!TextUtils.isEmpty(mNo)) {
                loadSubTabData();
            } else {
                loadData(false);
            }
        } else {
            mNo = "";
            mBinding.tabLayout.setVisibility(View.GONE);
            loadData(false);
        }
    }

    public void setSubTabLayoutData(List<CommonPopEntity> data) {
        mNo11 = "";
        if (data == null || data.size() == 0) {
            mBinding.tabLayout2.setVisibility(View.GONE);
            loadData(false);
        } else {
            mBinding.tabLayout2.removeAllTabs();
            mBinding.tabLayout2.clearOnTabSelectedListeners();
            mTabData11 = new ArrayList<>();
            mTabData11.addAll(data);
            mBinding.tabLayout2.setVisibility(View.VISIBLE);
            mBinding.tabLayout2.setTabMode(TabLayout.MODE_SCROLLABLE);
            for (int i = 0; i < mTabData11.size(); i++) {
                mBinding.tabLayout2.addTab(mBinding.tabLayout2.newTab().setText(mTabData11.get(i).getTypeName()));
            }
            mBinding.tabLayout2.setNormalColor(R.color.color_tab_three);
            mBinding.tabLayout2.addOnTabSelectedListener(mTabData11, new XCustomTabLayout.SimpleSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    mNo11 = getSubTabNo(tab.getPosition());
                    loadData(false);
                }
            });
            mBinding.tabLayout2.scrollTo(0, 0);
            mNo11 = getSubTabNo(0);
            loadData(false);
        }
    }

    public void showEmpty() {
        mBinding.recyclerView.setVisibility(View.INVISIBLE);
        mBinding.emptyView.setVisibility(View.VISIBLE);
    }

    public void closeRecyclerRefreshing() {
        mBinding.recyclerView.refreshComplete();
        mBinding.recyclerView.loadMoreComplete();
        Fragment parentFragment = getParentFragment();
        if (parentFragment instanceof HomeMemberFragment) {
            ((HomeMemberFragment) parentFragment).closeRecyclerRefreshing();
        }
    }

    public void refreshRecycler(List<MemberPersonBean> list, boolean isLoadMore, boolean noMoreData) {
        closeRecyclerRefreshing();
        if (mMemberAdapter == null) {
            return;
        }
        mBinding.recyclerView.setVisibility(View.VISIBLE);
        mBinding.recyclerView.setLoadingMoreEnabled(!noMoreData);
        this.noMoreData = noMoreData;
        if (isLoadMore) {
            if (list != null && list.size() > 0) {
                mMemberAdapter.addDatas(list);
            }
        } else {
            if (list != null && list.size() > 0) {
                mBinding.emptyView.setVisibility(View.GONE);
                mMemberAdapter.setDatas(list);
            } else {
                mMemberAdapter.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

}
